package cn.stronglink.asset.module.basicdata.assetBrand.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetBrand;
import cn.stronglink.asset.module.basicdata.assetBrand.mapper.AssetBrandMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class AssetBrandService {

	@Autowired
	private AssetBrandMapper assetBrandMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<AssetBrand> queryList(Page<AssetBrand> page,String name) {
		page.setRecords(assetBrandMapper.queryList(page,name));
		return page;	
	}
	
	public void removeAssetBrand(Long id){
		assetBrandMapper.removeAssetBrand(id);	
	}

	public void insertAssetBrand(AssetBrand assetBrand) {
		if (assetBrand.getId() == null) {
			assetBrand.setId(IdWorker.getId());
			assetBrand.setCreateBy(WebUtil.getCurrentUser());
			assetBrand.setCreateTime(new Date());
			assetBrandMapper.insertAssetBrand(assetBrand);		
		}else{
			assetBrand.setUpdateTime(new Date());
			assetBrand.setUpdateBy(WebUtil.getCurrentUser());
			assetBrandMapper.updateAssetBrand(assetBrand);
		}	
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetBrand> queryAssetBrandList() {
		return assetBrandMapper.queryAssetBrandList();	
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public long getBrandIDByName(String brandName){
		long brandId = assetBrandMapper.getBrandIdByName(brandName);
		if(brandId != 0){
			return brandId;
		}
		return 0;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public int getIsUsedBrand(Long id) {
		return assetBrandMapper.getIsUsedBrand(id);
	}

	

}
